#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QDirIterator>

#include "fileutils.h"

/*!
   Delete a directory along with all of its contents.

   \param dirName Path of directory to remove.
   \return true on success; false on error.
*/
bool FileUtils::removeDir(const QString &dirName)
{
    bool result = true;
    QDir dir(dirName);

    if (dir.exists(dirName)) {
        Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
            if (info.isDir()) {
                result = removeDir(info.absoluteFilePath());
            }
            else {
                result = QFile::remove(info.absoluteFilePath());
            }

            if (!result) {
                return result;
            }
        }
        result = dir.rmdir(dirName);
    }

    return result;
}

quint64 FileUtils::getDirSize(const QString &dirName)
{
    QDirIterator it(dirName,
                    QDir::Readable | QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot,
                    QDirIterator::Subdirectories );
    qint64 size = 0;
    while ( it.hasNext() )
    {
        it.next();
        size += it.fileInfo().size();
    }

    return size;
}

void FileUtils::copyOverwrite(const QString &fileName, const QString &newName)
{
    if(QFile::exists(newName)) {
        QFile::remove(newName);
    }
    QFile::copy(fileName, newName);
}
